#include "BON.h"
#include "BON2Component.h"
#include "stdafx.h"
#include "Library_Functions.h"

bool 
GOAL::objectExists (const std::string& path, BON::Folder& root)
{
  BON::Object object = getObjectByPath (path,root);

  return object != (BON::Object)NULL;
}

BON::Object 
GOAL::getObjectByPath (const std::string& path, BON::Folder& folder)
{
  BON::Object returnValue = NULL;

  std::set <::BON::Model> models = folder->getChildModels ();
  std::set <::BON::Model>::iterator i = models.begin ();

  AfxMessageBox(folder->getPath ("").c_str ());

  for (; i != models.end (); ++i)
    {
      std::string iPath ( (*i)->getPath ("."));
      iPath.erase(1);

      // way too much recursion going on if we don't do this
      if (path.find(iPath) != std::string::npos)
        {
          returnValue = getObjectByPath (path,*i,folder);
          if (returnValue != (BON::Object)NULL)
          {
            return returnValue;
          }
        }
    }
  return returnValue;
}

BON::Object 
GOAL::getObjectByPath (const std::string& path, 
                       BON::Model& current, BON::Object& path_relative_to)
{
  BON::Object returnValue = NULL;

  std::string curPath = current->getPath (".",false,false);
  std::string rootPath = path_relative_to->getPath (".",false,false);

  curPath = curPath.erase (0, rootPath.length () + 1);

//  std::string curPath = current.getPath (".",false,false,"name",true);
//  std::string rootPath = path_relative_to.getPath (".",false,false,"name",true);

//  curPath = curPath.erase (0, rootPath.length () + 1);

//  curPath = "CurPath: " + curPath + "\n";
 // rootPath = "RootPath: " + rootPath + "\n";

//  curPath += rootPath;



  if (path == curPath)
    {
      AfxMessageBox ("M: Found!");
        return current;
    }

  // search through the modules for a path match

  std::set <BON::Model> models = current->getChildModels ();
  for (std::set <BON::Model>::iterator i = models.begin ();
       i != models.end ();
       ++i)
    {
      std::string iPath ( (*i)->getPath ("."));
      iPath.erase(1);

      // way too much recursion going on if we don't do this
      if (path.find(iPath) != std::string::npos)
        {
          returnValue = getObjectByPath (path,*i, path_relative_to);
          if (returnValue != (BON::Object)NULL)
            {
             return returnValue;
            }
        }
    }

  return returnValue;
}

std::string
GOAL::getParentPath (const std::string& path)
{
  std::string::size_type last = path.find_last_of (".");

  return path.substr (0, last);
}

/*
void
GOAL::Library::read (const std::string& name, Udm::SmartDataNetwork& library)
{
//  Udm::SmartDataNetwork library (GOAL::diagram);

  std::string library_file (getenv("GOAL_ROOT"));

  // library directory
  library_file += "/library";

  // primitives
  library_file += "/" + name + ".mga";

	AfxMessageBox(library_file.c_str ());

  library.OpenExisting (library_file.c_str ());

//  root = GOAL::RootFolder::Cast (library.GetRootObject ());

  std::set <ModuleAdapter> modules = root.ModuleAdapter_kind_children ();

  for (std::set <ModuleAdapter>::iterator i = modules.begin ();
        i != modules.end ();
        ++i)
        AfxMessageBox((*i).name ().operator std::string ().c_str ());

}*/
